package com.xbongbong.pro.rabbitmq.producer.impl;

import com.xbongbong.pro.businessrule.pojo.dto.OperatorBatchDTO;
import com.xbongbong.pro.rabbitmq.binders.BusinessRuleBinder;
import com.xbongbong.pro.rabbitmq.producer.BusinessRuleProducer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.context.annotation.Lazy;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @author 吴峰
 * @version v1.0
 * @date 2019/5/7 14:43
 * @since v1.0
 */
@Service("businessRuleProducer")
@EnableBinding(BusinessRuleBinder.class)
public class BusinessRuleProducerImpl implements BusinessRuleProducer {

    private static final Logger LOG = LoggerFactory.getLogger(BusinessRuleProducerImpl.class);
    @Resource
    @Lazy
    private BusinessRuleBinder businessRuleBinder;

    @Override
    public void sendMessage(OperatorBatchDTO operationDTO) {
        try {

            MessageBuilder messageBuilder = MessageBuilder.withPayload(operationDTO)
//                    .setHeader(AmqpHeaders.CHANNEL, Channel.class)
//                    .setHeader(AmqpHeaders.DELIVERY_TAG, 1)
                    ;
            if (!Objects.equals(1, operationDTO.getIsImport())) {
                businessRuleBinder.ruleOutput().send(messageBuilder.build());
            } else {
                // 导入触发业务规则
                businessRuleBinder.importRuleOutput().send(messageBuilder.build());
            }
        } catch (Exception e) {
            LOG.error("rule message fail：", e);
        }

    }
}
